﻿using RestSharp;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Security;

namespace Kanban.Api
{
    public class Helpers
    {
        public static string EncodeMD5(string value)
        {
            var md5Provider = new MD5CryptoServiceProvider();
            var originalBytes = Encoding.Default.GetBytes(value);
            var hash = md5Provider.ComputeHash(originalBytes);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            return sb.ToString();
        }

        public static T WebService<T>(string resource, Method metodo) where T : new()
        {
            var client = new RestClient(ConfigurationManager.AppSettings["ApiUrl"]);
            var request = new RestRequest(resource, metodo);
            request.AddCookie(FormsAuthentication.FormsCookieName, HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName].Value);

            IRestResponse<T> response = client.Execute<T>(request);

            if (response.StatusCode != HttpStatusCode.OK)
                throw new Exception(response.Content);

            return response.Data;
        }

      
        public static string Cifrar(string valor)
        {
            string chave = ConfigurationManager.AppSettings["Chave"];

            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(valor);

            TripleDESCryptoServiceProvider tDes = new TripleDESCryptoServiceProvider();
            tDes.Key = UTF8Encoding.UTF8.GetBytes(chave);
            tDes.Mode = CipherMode.ECB;
            tDes.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = tDes.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            tDes.Clear();
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        public static string Decifrar(string valor)
        {
            string chave = ConfigurationManager.AppSettings["Chave"];

            byte[] toDecryptArray = Convert.FromBase64String(valor);

            TripleDESCryptoServiceProvider tDes = new TripleDESCryptoServiceProvider();
            tDes.Key = UTF8Encoding.UTF8.GetBytes(chave);
            tDes.Mode = CipherMode.ECB;
            tDes.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = tDes.CreateDecryptor();
            try
            {
                byte[] resultArray = cTransform.TransformFinalBlock(toDecryptArray, 0, toDecryptArray.Length);

                tDes.Clear();
                return UTF8Encoding.UTF8.GetString(resultArray, 0, resultArray.Length);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        
    }
}